package com.teamit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.teamit.entity.Project;
import com.teamit.util.db.DBHelp;
import com.teamit.util.db.RowMapper;

public class ProjectDao {
	DBHelp<Project> db=new DBHelp<Project>();
	public void save(Project project){
		String sql="insert into t_project(id,`name`,`desc`) values(?,?,?)";
		db.executeSQL(sql,project.getId(),project.getName(),project.getDesc());
	}
	public List<Project> findProjectByEmployeeId(String employeeId){
		StringBuffer sb=new StringBuffer();
		sb.append("select id,`name`,`desc`,createtime from t_project,t_employee_project ").append
		("WHERE t_project.id=t_employee_project.project_id").append(" and t_employee_project.employee_id=? order by createtime desc");
		String sql=sb.toString();
		return db.executeQueryToList(sql, new Mapper(), employeeId);
	}
	public Project findByEmployeeId(String employeeId,String projectId){
		StringBuffer sb=new StringBuffer();
		sb.append("select id,`name`,`desc`,createtime from t_project,t_employee_project ").append
		("WHERE t_project.id=t_employee_project.project_id").append(" and t_employee_project.employee_id=? and t_project.id=?");
		String sql=sb.toString();
		return db.executeQueryToObject(sql, new Mapper(), employeeId,projectId);
	}
	private class Mapper implements RowMapper<Project>{

		public Project mapRow(ResultSet rs) throws SQLException {
			Project project=new Project();
			project.setId(rs.getString("id"));
			project.setName(rs.getString("name"));
			project.setDesc(rs.getString("desc"));
			project.setCreatetime(rs.getString("createtime"));
			return project;
		}}
}
